package com.mobileares.midp.widgets.client.textbox2;

import com.google.gwt.user.client.ui.Widget;
import com.mobileares.midp.widgets.client.textbox2.BaseLabelText;
import com.mobileares.midp.widgets.client.utils.AbstractCodeProvider;
import com.mobileares.midp.widgets.client.utils.CodeModel;
import com.mobileares.midp.widgets.client.utils.ICodeProvider;

import java.util.Iterator;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Tom
 * Date: 2011-11-24
 * Time: 16:55:02
 * To change this template use File | Settings | File Templates.
 */
public abstract class BaseLabelList<T, U extends Widget>  extends BaseLabelText<T,U>{
    private boolean isNull;
    public BaseLabelList(String label) {
        this(label,false);
    }
    public BaseLabelList(String label,boolean isNull) {
        super(label);
        this.isNull = isNull;
    }
    //default convert
    protected AbstractCodeProvider codeConvertProvider =new AbstractCodeProvider<CodeModel>(){

        public String getText(CodeModel ob) {
            return ob.getText();
        }

        public String getCode(CodeModel ob) {
            return ob.getCode();
        }
    };


    public void initListCodes(List codes) {
        if(isNull){
              addNullItem();
        }
        if (codes == null) return;
        else
            for (Iterator it = codes.iterator(); it.hasNext();) {
                Object o = it.next();
                addItem(o);
            }
    }

    public void setCodeConvertProvider(AbstractCodeProvider provider) {
        this.codeConvertProvider = provider;
    }

    protected abstract void addItem(Object ob);
    protected abstract void addNullItem();
}
